package com.jnetwork.servlet.idea;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;

import com.jnetwork.dao.IdeaDAO;
import com.jnetwork.exception.DAOExcepcion;
import com.jnetwork.modelo.Idea;
import com.jnetwork.negocio.GestionIdea;

/**
 * Servlet implementation class IdeaCrearServlet
 */
@WebServlet("/IdeaCrearServlet")
public class IdeaCrearServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdeaCrearServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
	
	String  titulo = request.getParameter("txtTitulo");
	String  descripcion = request.getParameter("txtDescripcion");
	String  estado = request.getParameter("txtEstado");
	String  archivo = request.getParameter("txtArchivo");
	String  key1 = request.getParameter("txtKey1");
	String  key2 = request.getParameter("txtKey2");
	String  key3 = request.getParameter("txtKey3");
	String  key4 = request.getParameter("txtKey4");
	
	GestionIdea negocio = new GestionIdea();
	Collection<Idea> lista = null;
	try {
		lista = negocio.listar(titulo,estado);
	} catch (DAOExcepcion e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	try {
			if (lista == null) {
			Idea c;
			
				c = negocio.crear(titulo, descripcion, estado, archivo, new Date(), key1, key2, key3, key4, 2);
				RequestDispatcher rd = request.getRequestDispatcher("ideas_buscar.jsp");
				rd.forward(request, response);
				
			}
			else {
				
				//GUARDAR EN MEMORIA
				RequestDispatcher rd = request.getRequestDispatcher("IdeaError.jsp");
				rd.forward(request, response);
			}
			
	} catch (DAOExcepcion e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
						
}
	

}